package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.R;
import com.itheima.api.PmsProductAttributeCategoryApi;
import com.itheima.dto.PmsProductAttributeCategoryItem;
import com.itheima.pojo.db.PmsProductAttributeCategory;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chensen
 * @create 2022/10/12/20:13
 */
@Service
public class PmsProductAttributeCategoryService {
    @DubboReference
    private PmsProductAttributeCategoryApi pmsProductAttributeCategoryApi;

    public R create(String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        int count = pmsProductAttributeCategoryApi.insert(productAttributeCategory);
        if (count > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    public R delete(Long id) {
        int count = pmsProductAttributeCategoryApi.delete(id);
        if (count > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    public R list(Integer pageNum, Integer pageSize) {
        IPage<PmsProductAttributeCategory> page = new Page<>(pageNum, pageSize);
        IPage<PmsProductAttributeCategory> pageL = pmsProductAttributeCategoryApi.selectPage(page);

        return R.success(PageResult.init(pageL));
    }

    public R getAll() {
        List<PmsProductAttributeCategoryItem> item = pmsProductAttributeCategoryApi.select();
        return R.success(item);
    }


    public R update(Long id, String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setId(id);
        productAttributeCategory.setName(name);
        int count = pmsProductAttributeCategoryApi.updateById(productAttributeCategory);
        if (count > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败！");
        }
    }

    public R getItem(Long id) {
        PmsProductAttributeCategory productAttributeCategory = pmsProductAttributeCategoryApi.selectOne(id);
        return R.success(productAttributeCategory);
    }

}
